<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>仿朋友圈九宫格纯css</title>
    <style>
        .contaner{
                width: 350px;
                margin: 0px auto;
        }
        .imgbox {
            font-size: 0;
        }
        .imgbox .item{
            background-position: center;
            background-size: cover;
            background-repeat: no-repeat;
            display: inline-block;
            transition: all 0.5s;
        }
        /* 一张图片 */
        .imgbox .item:only-child{
            width: 80%;
            height: calc(350px * 0.8);
        }
        /* 两张图片 */
        .imgbox .item:first-child:nth-last-child(2),.imgbox .item:first-child:nth-last-child(2) ~ .item{
            width:  calc((350px /2) - 5px);
            height: calc(350px / 2);;
        }
        .imgbox .item:first-child:nth-last-child(2) ~ .item{
            margin-left: 5px;
        }
        /* 三张图片 */
        .imgbox .item:first-child:nth-last-child(3) ~ .item,.imgbox .item:first-child:nth-last-child(3){
            width:  calc((350px - 5px - 5px) / 3);
            height:   calc((350px - 5px - 5px) / 3);
        }
        .imgbox .item:first-child:nth-last-child(3) ~ .item{
            margin-left: 5px;
        }
        /* 四张图片 */
        .imgbox .item:first-child:nth-last-child(4) ~ .item,.imgbox .item:first-child:nth-last-child(4){
            width:  calc((350px - 5px) / 2);
            height:  calc((350px - 5px) / 2);
        }
        .imgbox .item:nth-child(2):nth-last-child(3),.imgbox .item:nth-child(4):nth-last-child(1){
               margin-left: 5px;
        }
        .imgbox .item:nth-child(3):nth-last-child(2),.imgbox .item:nth-child(4):nth-last-child(1){
                margin-top: 5px;
        }
        /* 五张图片以上 */

        .imgbox .item:first-child:nth-last-child(n+5),.imgbox .item:first-child:nth-last-child(n+5) ~ .item{
            width:  calc((350px - 5px - 5px) / 3);
            height:  calc((350px - 5px -5px) / 3);
        }
        .imgbox .item:nth-child(3n-1),.imgbox .item:nth-child(3n){
            margin-left: 5px;
        }
        .imgbox .item:nth-child(3):nth-child(3n+3) ~ .item{
            margin-top: 5px;
        }

        /* 排除四张图的时候第三张出现的左边距 */
        .imgbox .item:nth-child(3):nth-last-child(2){
            margin-left: 0px;
        }


    </style>
</head>
<body>
    <!-- 按照设计图宽度350px设计的 图片间距5px  -->
    <div class="contaner">

        <p>一张图</p>
        <div class="imgbox">
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohiJr4.jpg');"></div>
        </div>

        <p>两张图</p>
        <div class="imgbox">
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohiJr4.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBt3R.jpg');"></div>
        </div>
        <p>三张图</p>
        <div class="imgbox">
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohiJr4.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBt3R.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBUjx.jpg');"></div>
        </div>
        <p>四张图</p>
        <div class="imgbox">
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohiJr4.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBt3R.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBUjx.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBNg1.jpg');"></div>
        </div>
        <p>五张图</p>
        <div class="imgbox">
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohiJr4.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBt3R.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBUjx.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBNg1.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBdu6.jpg');"></div>
        </div>

        <p>五张以上</p>
        <div class="imgbox">
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohiJr4.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBt3R.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBUjx.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBNg1.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBdu6.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBYC9.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBxVU.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBXrV.jpg');"></div>
            <div class="item" style="background-image: url('https://s1.ax1x.com/2021/12/09/ohBOK0.jpg');"></div>
        </div>
           
    </div>
</body>
</html>